<template>
    <jb-crud-page
        ref="appPage"
        title-icon="icon-park-outline:more-app"
        title-text="应用开发中心"
        search-url="/api/admin/app/datas"
        :search-conditions="pageConditions"
        :edit-component="AppEdit"
    >
        <template #conditions-form>
            <n-input
                v-model:value="pageConditions.keywords"
                type="text"
                placeholder="输入关键字搜索"
            />
            <jb-select
                v-model:value="pageConditions.type"
                url="/api/admin/app/types"
                placeholder="=应用类型="
                filterable
                class="w-130px"
            >
            </jb-select>
            <jb-select
                v-model:value="pageConditions.enable"
                url="/api/admin/dictionary/options?typeKey=options_enable"
                placeholder="=状态="
                filterable
                class="w-130px"
            >
            </jb-select>
        </template>
        <template #opt>
            <jb-btn
                :icon="Icons.ADD"
                ghost
                type="primary"
                @click="appPage?.showEditModal('创建新应用')"
            >
                创建新应用
            </jb-btn>
        </template>
        <template #default="{ state }">
            <vxe-table
                border
                :column-config="{ resizable: true }"
                height="auto"
                show-header-overflow
                show-overflow="tooltip"
                :seq-config="{ startIndex: state.tableStartIndex }"
                :row-config="{ isHover: true }"
                align="center"
                :data="state.list"
            >
                >
                <jb-column type="seq" title="序号" width="60" fixed="left"></jb-column>
                <jb-column
                    field="name"
                    title="应用名称"
                    min-width="240"
                    fixed="left"
                ></jb-column>
                <jb-column field="typeName" title="类型" min-width="200"></jb-column>
                <jb-column title="AppId/AppSecret" width="150">
                    <template #default="{ row }">
                        <jb-btn
                            :icon="Icons.EYE"
                            type="primary"
                            ghost
                            :modal-title="`查看【${row.name}】的开发配置`"
                            :modal-component="AppSecret"
                            :modal-args="{ negativeText: '', positiveText: '' }"
                            :modal-component-props="{ row }"
                            @modalNoClick="refresh"
                            >查看</jb-btn
                        >
                    </template>
                </jb-column>
                <jb-column title="简介" width="120">
                    <template #default="{ row }">
                        <jb-btn
                            :icon="Icons.EYE"
                            type="primary"
                            ghost
                            :modal-title="`查看${row.name}简介`"
                            :modal-component="AppBrief"
                            :modal-args="{ negativeText: '', positiveText: '' }"
                            :modal-component-props="{ brief: row.briefInfo }"
                            >查看</jb-btn
                        >
                    </template>
                </jb-column>
                <jb-column
                    field="createTime"
                    title="创建时间"
                    min-width="200"
                ></jb-column>
                <jb-column
                    field="updateTime"
                    title="更新时间"
                    min-width="200"
                ></jb-column>
                <jb-column title="关联对象" min-width="200"> </jb-column>
                <jb-column field="enable" title="是否启用" width="160" fixed="right">
                    <template #default="{ row }">
                        <jb-switch
                            v-model:value="row.enable"
                            :url="`/api/admin/app/toggleEnable/${row.id}`"
                        ></jb-switch>
                    </template>
                </jb-column>
                <jb-column field="needCheckSign" title="接口校验Signature"  fixed="right" width="160">
                    <template #default="{ row }">
                        <jb-switch
                            v-model:value="row.needCheckSign"
                            field-name="needCheckSign"
                            :url="`/api/admin/app/toggleNeedCheckSign/${row.id}`"
                        ></jb-switch>
                    </template>
                </jb-column>

                <jb-column title="操作" width="140" fixed="right">
                    <template #default="{ row }">
                        <jb-btn
                            secondary
                            type="warning"
                            tip-text="编辑"
                            :icon="Icons.EDIT"
                            circle
                            @click="
                                appPage?.showEditModal('编辑应用基本信息', {
                                    id: row.id
                                })
                            "
                        ></jb-btn>
                        <jb-btn
                            secondary
                            type="error"
                            tip-text="删除"
                            :icon="Icons.DELETE"
                            circle
                            class="mx-8px"
                            confirm-text="确定删除这条数据？"
                            :url="`/api/admin/app/delete/${row.id}`"
                            @success="appPage?.loadData()"
                        ></jb-btn>
                    </template>
                </jb-column>
            </vxe-table>
        </template>
    </jb-crud-page>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Icons } from '@/constants'
import JbCrudPage from '@/components/_builtin/jb-crud-page/index.vue'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import { DynamicComponentEmitArgs } from '@/typings/jbolt.components'
import AppBrief from './components/app-brief/index.vue'
import AppEdit from './components/app-edit/index.vue'
import AppSecret from './components/app-secret/index.vue'

const appPage = ref<InstanceType<typeof JbCrudPage> | null>(null)
const pageConditions = useResetableData({
    keywords: '',
    type: '',
    enable: ''
})
function refresh({ close }: DynamicComponentEmitArgs) {
    if (appPage.value) appPage.value.loadData()
    close()
}
</script>

<style scoped></style>
